import 'package:check_app/dao/login_dao.dart';
import 'package:check_app/scoped_model/app_model.dart';
import 'package:check_app/util/pref_util.dart';
import 'package:flutter/material.dart';
import 'package:check_app/navigate/navigate.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:modal_progress_hud/modal_progress_hud.dart';
import 'package:getuiflut/getuiflut.dart';
import 'package:scoped_model/scoped_model.dart';

class LoginPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => new _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  bool _loading = false;
  String username;
  String password;
  TextEditingController _usernameController = TextEditingController();
  TextEditingController _passwordController = TextEditingController();

  @override
  void initState() {
    super.initState();
    // _usernameController.text = 'admin';
    // _passwordController.text = '123';
    _usernameController.addListener(() {
      setState(() {
        username = _usernameController.text;
      });
    });
    _passwordController.addListener(() {
      setState(() {
        username = _passwordController.text;
      });
    });
  }

  Future<Null> login() async {
    setState(() {
      _loading = true;
    });
    try {
      var result = await LoginDao.login({
        'code': _usernameController.text,
        'password': _passwordController.text,
      });
      setState(() {
        _loading = false;
      });
      if (result.success) {
        PrefUtil().setBool('isLogin', true);
        PrefUtil().setString('token', result.token);
        PrefUtil().setString('aid', result.data.aid);
        PrefUtil().setString('name', result.data.name);
        Getuiflut().bindAlias(result.data.aid, null);
        Navigator.of(context).pushReplacement(MaterialPageRoute(builder: (_) {
          return Navigete();
        }));
      } else {
        Fluttertoast.showToast(
          msg: "登录失败",
        );
      }
    } catch (e) {
      print(e);
      setState(() {
        _loading = false;
      });
      Fluttertoast.showToast(
        msg: "工号或密码错误",
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.CENTER,
        timeInSecForIos: 1,
        backgroundColor: Colors.red,
        textColor: Colors.white,
        fontSize: 16.0,
      );
    }
    return null;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        resizeToAvoidBottomInset: false,
        // resizeToAvoidBottomPadding: false,
        backgroundColor: Color.fromRGBO(255, 255, 255, 1),
        body: ModalProgressHUD(
          inAsyncCall: _loading,
          color: Colors.black,
          child: Stack(
            children: <Widget>[
              Column(
                children: <Widget>[
                  // logo
                  Container(
                    margin: EdgeInsets.fromLTRB(138.5, 56, 141.5, 32),
                    child: Image(
                      image: AssetImage('assets/images/logo.png'),
                      width: 80,
                      height: 80,
                    ),
                  ),
                  // 工号
                  Container(
                    // color: Colors.deepOrange,
                    padding: EdgeInsets.fromLTRB(32, 0, 32, 0),
                    child: Column(
                      children: <Widget>[
                        Container(
                          alignment: Alignment.centerLeft,
                          margin: EdgeInsets.only(
                            bottom: 0,
                          ),
                          child: Text(
                            '工号',
                            style: TextStyle(
                              fontSize: 12,
                              height: 16.5 / 12,
                              color: Color.fromRGBO(88, 131, 252, 1),
                            ),
                          ),
                        ),
                        Container(
                          // color: Colors.orange,
                          child: TextField(
                            controller: _usernameController,
                            style: TextStyle(
                              fontSize: 16,
                              height: 22 / 16,
                              color: Color.fromRGBO(43, 44, 46, 1),
                            ),
                            decoration: InputDecoration(
                              // contentPadding: EdgeInsets.fromLTRB(),
                              hintText: "请输入用户工号",
                              hintStyle: TextStyle(
                                fontSize: 16,
                                height: 16 / 22,
                                color: Color.fromRGBO(181, 184, 189, 1),
                              ),
                              suffix: GestureDetector(
                                onTap: () {
                                  setState(() {
                                    _usernameController.text = '';
                                  });
                                },
                                child: Container(
                                  child: Image(
                                    image:
                                        AssetImage('assets/images/clear.png'),
                                    width: 16,
                                    height: 16,
                                  ),
                                ),
                              ),
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                  // 密码
                  Container(
                    // color: Colors.deepOrange,
                    padding: EdgeInsets.fromLTRB(32, 0, 32, 0),
                    child: Column(
                      children: <Widget>[
                        Container(
                          alignment: Alignment.centerLeft,
                          margin: EdgeInsets.only(
                            bottom: 0,
                            top: 17.5,
                          ),
                          child: Text(
                            '密码',
                            style: TextStyle(
                              fontSize: 12,
                              height: 16.6 / 12,
                              color: Color.fromRGBO(88, 131, 252, 1),
                            ),
                          ),
                        ),
                        Container(
                          // color: Colors.orange,
                          child: TextField(
                            obscureText: true,
                            controller: _passwordController,
                            style: TextStyle(
                              fontSize: 16,
                              height: 22 / 16,
                              color: Color.fromRGBO(43, 44, 46, 1),
                            ),
                            decoration: InputDecoration(
                              // contentPadding: EdgeInsets.fromLTRB(),
                              hintText: "请输入密码",
                              hintStyle: TextStyle(
                                fontSize: 16,
                                height: 22 / 16,
                                color: Color.fromRGBO(181, 184, 189, 1),
                              ),
                              suffix: GestureDetector(
                                onTap: () {
                                  setState(() {
                                    _passwordController.text = '';
                                  });
                                },
                                child: Container(
                                  child: Image(
                                    image:
                                        AssetImage('assets/images/clear.png'),
                                    width: 16,
                                    height: 16,
                                  ),
                                ),
                              ),
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                  // 登录按钮
                  Container(
                    margin: EdgeInsets.only(
                      top: 32,
                    ),
                    padding: EdgeInsets.fromLTRB(32, 0, 32, 0),
                    child: MaterialButton(
                      onPressed: () {
                        FocusScope.of(context).requestFocus(FocusNode());
                        login();
                      },
                      color: (_usernameController.text != '') &&
                              (_passwordController.text != '')
                          ? Colors.blue
                          : Colors.grey,
                      textColor: Colors.white,
                      minWidth: 296,
                      height: 44,
                      child: Text(
                        '登录',
                        style: TextStyle(
                          fontSize: 17,
                          height: 24 / 17,
                        ),
                      ),
                    ),
                  ),
                ],
              ),
              Positioned(
                left: 0,
                bottom: 0,
                right: 0,
                child: Container(
                  height: 24,
                  alignment: Alignment.center,
                  child: ScopedModelDescendant<GlobalModel>(
                    rebuildOnChange: true, //改变的时候是否重建
                    builder: (context, _, model) => Text(model.currentVersion),
                  ),
                ),
              ),
            ],
          ),
        ));
  }
}
